package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/largeScreen")
public class LargeScreen extends BaseController {

    @Autowired
    private ITbComponentinfoService tbComponentinfoService;

    @Autowired
    private ITbTestService tbTestService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private ITbStationStatusService tbStationStatusService;

    @Autowired
    private ITbLargScreenParametService iTbLargScreenParametService;

    @GetMapping("/aircraft")
    public String aircraft(HttpServletRequest request, HttpServletResponse response, ModelMap mmap)
    {
        TbComponentinfo tbComponentinfo=new TbComponentinfo();

        //获取配置信息
        String beginDate="",endDate="";
        //测试台统计分析-数据刷新时间
        int intervalTime=60000;
        //测试台统计分析-测试项数据刷新时间
        int waitTime=20000;
        String nowDate=DateUtils.getDate();

        List<TbLargScreenParamet> tbLargScreenParametList=iTbLargScreenParametService.selectTbLargScreenParametList(new TbLargScreenParamet());
        for(TbLargScreenParamet paramet :tbLargScreenParametList){
            if(StringUtils.equalsIgnoreCase(paramet.getName(),"P20220119")){
                if(StringUtils.isEmpty(DateUtils.dateTime(paramet.getParameter1()))){
                    beginDate=DateUtils.addMonth(nowDate,-3);
                }else{
                    beginDate= DateUtils.dateTime(paramet.getParameter1());
                }
                if(StringUtils.isEmpty(DateUtils.dateTime(paramet.getParameter2()))){
                    endDate=nowDate;
                }else{
                    endDate= DateUtils.dateTime(paramet.getParameter2());
                }
            }else if(StringUtils.equalsIgnoreCase(paramet.getName(),"P20190814") ){
                if(StringUtils.isEmpty(paramet.getParameter3())){}else{
                    intervalTime=Integer.valueOf(paramet.getParameter3());
                }
            }else if(StringUtils.equalsIgnoreCase(paramet.getName(),"P20201229") ){
                if(StringUtils.isEmpty(paramet.getParameter3())){}else{
                    waitTime=Integer.valueOf(paramet.getParameter3());
                }
            }
        }
        Map<String, Object> params = tbComponentinfo.getParams();
        params.put("beginTime", beginDate);
        params.put("endTime", endDate);
        tbComponentinfo.setParams(params);

        mmap.put("time_interval1", beginDate+"至"+endDate);
        mmap.put("time_interval2", beginDate+"至"+endDate);
        mmap.put("time_interval3", beginDate+"至"+endDate);

        Map<String, String> totalMap=this.getTotalNumber(tbComponentinfo);
        mmap.put("zhufeikong_total", totalMap.get("zhufeikong_total"));
        mmap.put("zuodongtong_total", totalMap.get("zuodongtong_total"));
        mmap.put("yeyafa_total", totalMap.get("yeyafa_total"));

//        SysConfig config=new SysConfig();
//        config.setConfigKey(".refresh.time");
//        List<SysConfig> list = configService.selectConfigList(config);
//        for(SysConfig sysConfig:list){
//            if("data.statistical.refresh.time".equalsIgnoreCase(sysConfig.getConfigKey())){
//                intervalTime=Integer.valueOf(sysConfig.getConfigValue());
//            }else if("data.tbtest.refresh.time".equalsIgnoreCase(sysConfig.getConfigKey())) {
//                waitTime = Integer.valueOf(sysConfig.getConfigValue());
//            }
//        }

        mmap.put("intervalTime", intervalTime);
        mmap.put("activeTimeGap", intervalTime+2000);
        mmap.put("waitTime", waitTime);

        List<TbStationStatus> stationStatusList= tbStationStatusService.selectTbStationStatusList(new TbStationStatus());
        for(TbStationStatus status :stationStatusList){
            mmap.put("statusImg"+status.getStationId(), "/img/free.png");
            if("测试中".equals(status.getStatus())){
                mmap.put("statusImg"+status.getStationId(), "/img/test.png");
            }
            mmap.put("status"+status.getStationId(), status.getStatus());
        }
        return "system/largeScreen/aircraft";
    }
    private Map<String, String> getTotalNumber(TbComponentinfo tbComponentinfo){
        Map map=new HashMap<String, String>();
        int zhufeikong_total=0,zuodongtong_total=0,yeyafa_total=0;
        List<HashMap<String, Integer>> list=tbComponentinfoService.selectComponentinfoNoAndCount(tbComponentinfo);
        if(list.isEmpty()){}else{
            for(HashMap<String, Integer> tmpMap:list){
                if("主飞控测试台".equals(tmpMap.get("stationName"))){
                    zhufeikong_total=Integer.valueOf(tmpMap.get("count")+"");
                }else if("作动筒测试台".equals(tmpMap.get("stationName"))){
                    zuodongtong_total=Integer.valueOf(tmpMap.get("count")+"");
                }else if("液压阀测试台".equals(tmpMap.get("stationName"))){
                    yeyafa_total=Integer.valueOf(tmpMap.get("count")+"");
                }
            }
        }
        map.put("zhufeikong_total", zhufeikong_total);
        map.put("zuodongtong_total", zuodongtong_total);
        map.put("yeyafa_total", yeyafa_total);
        return map;

    }

    @Log(title = "获取统计数据", businessType = BusinessType.OTHER)
    @GetMapping("/getStatisticalData/{dataId}")
    @ResponseBody
    public AjaxResult getStatisticalData(@PathVariable("dataId") Integer dataId) {
        TbComponentinfo tbComponentinfo=new TbComponentinfo();
        if(dataId==1){
            tbComponentinfo.setStationName("主飞控测试台");
        }else if(dataId==2){
            tbComponentinfo.setStationName("作动筒测试台");
        }else if(dataId==3){
            tbComponentinfo.setStationName("液压阀测试台");
        }
        Map<String,Object> statisticalData=new HashMap<String,Object>();

        //获取图标数据
        String nowDate=DateUtils.getDate();
        String nowDate_0=DateUtils.dateTimeNow("yyyy-MM");
        String nowDate_1=DateUtils.addMonth(nowDate,-1);
        String nowDate_2=DateUtils.addMonth(nowDate,-2);
        //2.1 月份数据
        List<String> monthData=new ArrayList<String>(3);
        monthData.add(nowDate_2.substring(5,7)+"月");
        monthData.add(nowDate_1.substring(5,7)+"月");
        monthData.add(nowDate_0.substring(5,7)+"月");
        statisticalData.put("monthData",""+nowDate_2.substring(5,7)+"月      "+nowDate_1.substring(5,7)+"月      "+nowDate_0.substring(5,7)+"月");
        statisticalData.put("monthDataList",monthData);

        List<Map> chartsData=new ArrayList<>();
        Map<String,Object> map=null;

        //2.2 当月数据
        Map<String, Object> params=new HashMap<>(2);
        params.put("beginTime", nowDate_0+"-01");
        params.put("endTime", DateUtils.addDay(nowDate,1));
        tbComponentinfo.setParams(params);
        List<TbComponentinfo> tbComponentinfoList_0=tbComponentinfoService.selectTbComponentinfoList(tbComponentinfo);
        map=new HashMap<>();
        map.put("name",nowDate_0.substring(5,7)+"月");
        map.put("data",tbComponentinfoList_0.size());
        chartsData.add(map);

        //2.3 前一个月数据
        params=new HashMap<>(2);
        params.put("beginTime", nowDate_1+"-01");
        params.put("endTime", DateUtils.addDay(nowDate_0+"-01",-1));
        tbComponentinfo.setParams(params);
        List<TbComponentinfo> tbComponentinfoList_1=tbComponentinfoService.selectTbComponentinfoList(tbComponentinfo);

        map=new HashMap<>();
        map.put("name",nowDate_1.substring(5,7)+"月");
        map.put("data",tbComponentinfoList_1.size());
        chartsData.add(map);

        //2.4 前两个月数据
        params=new HashMap<>(2);
        params.put("beginTime", nowDate_2+"-01");
        params.put("endTime", DateUtils.addDay(nowDate_1+"-01",-1));
        tbComponentinfo.setParams(params);
        List<TbComponentinfo> tbComponentinfoList_2=tbComponentinfoService.selectTbComponentinfoList(tbComponentinfo);
        map=new HashMap<>();
        map.put("name",nowDate_2.substring(5,7)+"月");
        map.put("data",tbComponentinfoList_2.size());
        chartsData.add(map);

        statisticalData.put("chartsData",chartsData);

        return AjaxResult.success(statisticalData);
    }

    //获取成功的个数:判断所有的测试子项都=pass才算是这个测试成功
    private int getPassNumber(List<TbComponentinfo> tbComponentinfoList) {
        int pass_number=0;
        boolean ifPass=false;
        TbTest tbTest=null;
        List<TbTest> tbTestList;
        for(TbComponentinfo tbComponentinfo:tbComponentinfoList){
            ifPass=true;
            tbTest=new TbTest();
            tbTest.setReportNo(tbComponentinfo.getReportNo());
            tbTest.setStationName(tbComponentinfo.getStationName());
            tbTestList=tbTestService.selectTbTestList(tbTest);
            if(tbTestList.isEmpty()){
                ifPass=false;
            }else{
                for(TbTest tb:tbTestList){
                    if("pass".equals(tb.getTestSubItemIsPass())){
                    }else{
                        ifPass=false;
                        break;
                    }
                }
            }
            if(ifPass){
                pass_number++;
            }
        }
        return pass_number;
    }

    @Log(title = "获取测试件测试数据", businessType = BusinessType.OTHER)
    @GetMapping("/getTestData/{dataId}")
    @ResponseBody
    public AjaxResult getTestData(@PathVariable("dataId") Integer dataId) {
        TbComponentinfo tbComponentinfo=new TbComponentinfo();
        if(dataId==1){
            tbComponentinfo.setStationName("主飞控测试台");
        }else if(dataId==2){
            tbComponentinfo.setStationName("作动筒测试台");
        }else if(dataId==3){
            tbComponentinfo.setStationName("液压阀测试台");
        }
        List<TbTest> testList=null;
        List<TbComponentinfo> tbComponentinfoList=tbComponentinfoService.selectTbComponentinfoList(tbComponentinfo);
        if(tbComponentinfoList.isEmpty()){}else{
            String reportNo=tbComponentinfoList.get(0).getReportNo();
            TbTest tbTest=new TbTest();
            tbTest.setReportNo(reportNo);
            tbTest.setStationName(tbComponentinfo.getStationName());
            testList=tbTestService.selectTbTestList(tbTest);
        }

        return AjaxResult.success(testList);
    }

    @Log(title = "获取统计数据", businessType = BusinessType.OTHER)
    @GetMapping("/getPieChartData")
    @ResponseBody
    public AjaxResult getPieChartData(){
        //获取配置信息
        String beginDate="",endDate="";
        String nowDate=DateUtils.getDate();
        TbLargScreenParamet tbLargScreenParamet=new TbLargScreenParamet();
        tbLargScreenParamet.setName("P20220119");
        List<TbLargScreenParamet> tbLargScreenParametList=iTbLargScreenParametService.selectTbLargScreenParametList(tbLargScreenParamet);
        if(tbLargScreenParametList.isEmpty()){}else{
            TbLargScreenParamet paramet=tbLargScreenParametList.get(0);
            if(StringUtils.isEmpty(DateUtils.dateTime(paramet.getParameter1()))){
                beginDate=DateUtils.addMonth(nowDate,-3);
            }else{
                beginDate= DateUtils.dateTime(paramet.getParameter1());
            }
            if(StringUtils.isEmpty(DateUtils.dateTime(paramet.getParameter2()))){
                endDate=nowDate;
            }else{
                endDate= DateUtils.dateTime(paramet.getParameter2());
            }
        }
        Map<String,Map<String,Object>> picData=new HashMap<String,Map<String,Object>>(4);

        TbComponentinfo tbComponentinfo=new TbComponentinfo();
        Map<String, Object> params = tbComponentinfo.getParams();
        params.put("beginTime", beginDate);
        params.put("endTime", endDate);

        //时间段
        Map<String,Object> timeMap=new HashMap<String,Object>(2);
        timeMap.put("time",beginDate+"至"+endDate);
        picData.put("time_interval",timeMap);

        //1、主飞控测试台 获取图表信息
        Map<String,Object> zfk=new HashMap<>(3);
        tbComponentinfo.setStationName("主飞控测试台");
        List<TbComponentinfo> tbComponentinfoList_1=tbComponentinfoService.selectTbComponentinfoList(tbComponentinfo);
        int pass_number_1=this.getPassNumber(tbComponentinfoList_1);
        zfk.put("bt_total",Integer.valueOf(tbComponentinfoList_1.size()));
        zfk.put("bt_pass",pass_number_1);
        zfk.put("bt_no_pass",Integer.valueOf(tbComponentinfoList_1.size()) - pass_number_1);
        picData.put("zfk",zfk);

        //2、作动筒测试台 获取图表信息
        Map<String,Object> zdt=new HashMap<>(3);
        tbComponentinfo.setStationName("作动筒测试台");
        List<TbComponentinfo> tbComponentinfoList_2=tbComponentinfoService.selectTbComponentinfoList(tbComponentinfo);
        int pass_number_2=this.getPassNumber(tbComponentinfoList_2);
        zdt.put("bt_total",Integer.valueOf(tbComponentinfoList_2.size()));
        zdt.put("bt_pass",pass_number_2);
        zdt.put("bt_no_pass",Integer.valueOf(tbComponentinfoList_2.size()) - pass_number_2);
        picData.put("zdt",zdt);

        //3、液压阀测试台 获取图表信息
        Map<String,Object> yyf=new HashMap<>(3);
        tbComponentinfo.setStationName("液压阀测试台");
        List<TbComponentinfo> tbComponentinfoList_3=tbComponentinfoService.selectTbComponentinfoList(tbComponentinfo);
        int pass_number_3=this.getPassNumber(tbComponentinfoList_3);
        yyf.put("bt_total",Integer.valueOf(tbComponentinfoList_3.size()));
        yyf.put("bt_pass",pass_number_3);
        yyf.put("bt_no_pass",Integer.valueOf(tbComponentinfoList_3.size()) - pass_number_3);
        picData.put("yyf",yyf);

        return AjaxResult.success(picData);
    }

    @Log(title = "获取测试站状态", businessType = BusinessType.OTHER)
    @GetMapping("/getStationStatus/{dataId}")
    @ResponseBody
    public AjaxResult getStationStatus(@PathVariable("dataId") Integer dataId) {
        Map<String,String> statusMap=new HashMap<>(2);
//        TbStationStatus tbStationStatus=new TbStationStatus();
//        tbStationStatus.setStationId(Long.valueOf(dataId));
//        List<TbStationStatus> stationStatusList= tbStationStatusService.selectTbStationStatusList(tbStationStatus);
//        for(TbStationStatus status :stationStatusList){
//            statusMap.put("statusImg"+status.getStationId(), "/img/locked.png");
//            if("测试中".equals(status.getStatus())){
//                statusMap.put("statusImg"+status.getStationId(), "/img/progress.png");
//            }
//            statusMap.put("status"+status.getStationId(), status.getStatus());
//        }
//
        TbStationStatus tbStationStatus=tbStationStatusService.selectTbStationStatusById(Long.valueOf(dataId));
        statusMap.put("statusImg", "/img/free.png");
        if("测试中".equals(tbStationStatus.getStatus())){
            statusMap.put("statusImg", "/img/test.png");
        }
        statusMap.put("status", tbStationStatus.getStatus());

        return AjaxResult.success(statusMap);

    }
}
